;
(function (call) {
    var main = {
        userInfo: null,
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $(".eveningPaper .head label").text(userInfo.smuser.name);
            $("input").attr('autocomplete', 'off');
            var time = new Date().format('yyyy年MM月dd日');
            $("#publishTime").val(time);
            this.getOldData();
        },
        //一开始请求数据库中，没值，在请求初始化
        getOldData: function () {
            var that = this;
            var index = 1;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = productInfo.fortime
            var hour = time.split(':')[0];
            $(".list-right a").text(hour + '时');
            switch (hour) {
                case "05":
                    $(".sxbyb_tbody tr").show();
                    index = 1;
                    break;
                case "09":
                case "10":
                    $(".sxbyb_tbody tr").eq(0).hide().siblings().show();
                    index = 2;
                    break;
                case "15":
                    $(".sxbyb_tbody tr").eq(2).show().siblings().hide();
                    index = 3;
                    break;
            }
            
            var fcstTime = new Date().format('yyyy-MM-dd 08:00:00');
            if (hour == '15') {
                fcstTime = new Date().format('yyyy-MM-dd 20:00:00');
            }
            var params = {
                "product_id": productInfo.productId,
                "fortime": time,
                "queryTime": fcstTime
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (res.isSuccess && JSON.stringify(res.data) != "{}") {
                    var oldData = JSON.parse(res.data.data.content);
                    var g_sxbyb = "g_sxbyb0" + index + "_02";
                    var dataArr = JSON.parse(oldData.tempData.curTagData[g_sxbyb].val);
                    for (var i = 0; i < dataArr.length; i++) {
                        var indexEq = index === 1 ? i : index === 2 ? i + 1 : i + 2;
                        that.setInputVal(indexEq, dataArr[i]);
                    }
                    that.setOldShow(index, res.data);
                } else {
                    that.setViewByDate(hour);
                }
            })
        },
        //设置预览中文件的相关信息以及下载
        setOldShow: function (index, data) {
            var that = this;
            var filePathArr = data.data.fileName.split(";");
            $(".fileNum").show();
            switch (index) {
                case 1:      //早晨
                    $(".fileNum >div").eq(0).data("content", data.txtContent).data("filePath", filePathArr[0]);
                    $(".fileNum >div").eq(1).data("content", data.txtContent2).data("filePath", filePathArr[1]);
                    $(".fileNum >div").eq(2).data("content", data.txtContent3).data("filePath", filePathArr[2]);
                    break;
                case 2:      //中午
                    $(".fileNum >div").eq(0).hide();
                    $(".fileNum >div").eq(1).data("content", data.txtContent2).data("filePath", filePathArr[0]);
                    $(".fileNum >div").eq(2).data("content", data.txtContent3).data("filePath", filePathArr[1]);
                    break;
                case 3:      //下午
                    $(".fileNum >div").eq(0).hide();
                    $(".fileNum >div").eq(1).hide();
                    $(".fileNum >div").eq(2).data("content", data.txtContent3).data("filePath", filePathArr[0]);
                    break;
            }
            $(".fileNum >div").eq(index - 1).click();
        },
        setViewByDate: function (hour) {
            var that = this;
            switch (hour) {
                case "05":
                    that.getSxbybData([{ "hour": hour, "startPeriod": 11, "endPeriod":12 }, { "hour": hour, "startPeriod": 15, "endPeriod": 16 }, { "hour": hour, "startPeriod": 21, "endPeriod": 22 }]);
                    break;
                case "09":
                case "10":
                    that.getSxbybData([{ "hour": hour, "startPeriod": 15, "endPeriod": 16 }, { "hour": hour, "startPeriod": 21, "endPeriod": 22 }]);
                    break;
                case "15":
                    that.getSxbybData([{ "hour": hour, "startPeriod": 21, "endPeriod": 22 }]);
                    break;
            }
        },

        getParams: function (hour, startPeriod, endPeriod) {
            var that = this;
            var currentDate = new Date();
            currentDate.setHours(currentDate.getHours() - 24);
            var fcstTime = currentDate.format('yyyyMMdd200000');
            var params = {
                fcstTime: fcstTime,
                element: 'EDA,WEA,TMP,RH',
                startPeriod: startPeriod,
                endPeriod: endPeriod,
                interval: 1,
                stationType: 0,
                lon: "117.21",
                lat: "39.07"
            }
            return params;
        },
        getSxbybData: function (arr) {
            var that = this;
            for (let i = 0; i < arr.length; i++) {
                var params = that.getParams(arr[i].hour, arr[i].startPeriod, arr[i].endPeriod);
                that.initSxbybData(params, i, arr.length);
            }
        },
        initSxbybData: function (params, index, length) {
            var that = this;
            loading.add('.cafe-container');
            index = length === 3 ? index : length === 2 ? index + 1 : index + 2;
            $.getJSON(api.external.getGdybDataByParamData, params, function (res) {
                loading.remove();
                if (!res.success || !res.resultData) return;
                var resultData = res.resultData['117.21_39.07'];
				var rh=resultData.RH[0][0] == '9999' ? '--' : parseInt(resultData.RH[0][0]);
				var tmp=resultData.TMP[0][0] == '9999' ? '--' : resultData.TMP[0][0];
                that.setInputVal(index, ["", resultData.WEA[0][0], resultData.EDA[2][0], resultData.EDA[3][0], rh, tmp]);
            })
        },
        setInputVal: function (index, arr) {
            for (let i = 1; i < arr.length; i++) {
                $(".sxbyb_tbody tr").eq(index).find("td").eq(i).find("input").val(arr[i]);
            }
        },
        //发布
        publishSxbyb: function () {
            var that = this;
            var productObj = $(".task-menu-list li.active").data('product');
            var dataVal = [];
            $(".sxbyb_tbody tr").each(function () {
                var arr = [];
                if ($(this).css("display") !== "none") {
                    $(this).find("td input").each(function () {
                        arr.push($(this).val());
                    })
                    dataVal.push(arr);
                }
            })
            var index = dataVal.length === 1 ? 3 : dataVal.length === 3 ? 1 : dataVal.length;
            var g_sxbyb = "g_sxbyb0" + index + "_02";
            var i_sxbyb = "i_sxbyb0" + index + "_01"
            var tempData = {
                "tempData": {
                    "curTagData": {
                    }
                }
            }
            tempData.tempData.curTagData[g_sxbyb] = {
                "val": JSON.stringify(dataVal)
            };
            tempData.tempData.curTagData[i_sxbyb] = {
                "val": $("#publishTime").val()
            };
            var params = {
                product_id: productObj.productId,
                fortime: productObj.fortime,
                forecasterId: that.userInfo.smuser.iid,
                jsonStr: JSON.stringify(tempData),
				taskId: productObj.taskId
            }
            $.ajax({
                url: api.internal.sxbyb.publishSxbyb,
                type: "POST",
                dataType: 'json',
                data: params,
                success: function (res) {
                    if (res.isSuccess) {
                        CafeJS.menuFunc.setTask();
                        that.getOldData();
                        alert("发布成功");
                    } else {
                        alert("发布失败");
                    }
                }
            })
        },
        //重做
        returnSxbtb: function () {
            var productInfo = $(".task-menu-list li.active").data('product');
            var hour = productInfo.fortime.split(':')[0];
            this.setViewByDate(hour);
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.publishSxbyb();
            })
            //重做
            $("#returnBtn").click(function () {
                that.returnSxbtb();
            })
            //点击文件
            $(".fileNum .txt").unbind("click").bind("click", function () {
                var obj = $(this);
                var content = obj.data("content");
                obj.addClass("active").siblings().removeClass("active");
                $("#proText").val(content);
            })
            //点击下载
            $("#downloadBtn").unbind("click").bind("click", function () {
                var obj = $(".fileNum .active");
                var path = obj.data("filePath");
                window.location.href = api.internal.common.downLoadFile + "?path=" + path;
            })
        }
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})